# -*- coding:utf-8 -*-
# @Time: 2020/8/24 17:43
# @Author: liupengfei
# @File: serializers.py
from django.conf import settings
from rest_framework import serializers

from utils.serializersfield import ImageUploadCharField
from .models import AdornProcess


class AdornProcessSerializer(serializers.ModelSerializer):
    relate_article_arr = serializers.SerializerMethodField()
    thumbnail_arr = ImageUploadCharField(source='thumbnail', read_only=True)
    thumbnail_path = serializers.SerializerMethodField(read_only=True)

    def get_relate_article_arr(self, obj):
        # 关联文章id数组
        return [article.id for article in obj.related_articles.all()]

    def get_thumbnail_path(self, obj):
        if obj.thumbnail:
            return obj.thumbnail.get_url()
        return ''

    class Meta:
        model = AdornProcess
        fields = '__all__'
